<?php
namespace app\home\model;

class Model{
    static private $instance; //静态变量不会随对象销毁 一直到电脑重启

    public  $sheet;
    private $link;

    private function __construct(){   //链接数据库
        $db = include "../config/config.db.php";
        $this->link = mysqli_connect($db['host'],$db['usr'],$db['pwd'],$db['db']);
        mysqli_set_charset($this->link,'utf8');
    }
    private function __clone(){}    //防止克隆

    static public function getInstance(){   //静态方法不能调用普通方法或属性 普通方法可以调用静态方法或属性
        if (!self::$instance instanceof self) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function sql($sql){  //操作数据库
        return mysqli_query($this->link,$sql);
    }
    
    public function get(){      //所有数据
        self::getInstance();
        $sql = "select * from `$this->sheet`";
        $re = $this->sql($sql);
        $arr = [];
        while($row = mysqli_fetch_assoc($re)){
            $arr[] = $row;
        }
        return $arr;
    }

    public function getNum(){      //计算数据总量
        $sql = "select count(*) as num from `$this->sheet`";
        $re = $this->sql($sql);
        $row = mysqli_fetch_assoc($re);
        return  $row['num'];
    }

    public function dele($id){  //删除
        $sql = "delete from `$this->sheet` where id = $id";
        $re = $this->sql($sql);
        return mysqli_affected_rows($this->link);
    }

    public function insert($data){  //添加
        $sql = "insert into `$this->sheet` (";
        foreach($data as $key=>$val){
            $sql .= "`$key`,";
        }
        $sql = substr($sql,0,-1).") values (";
        foreach ($data as $key => $val) {
            $sql .= "'$val',";
        }
        $sql = substr($sql,0,-1).")";
        return $this->sql($sql);
    }

    public function getOne($id){        //通过id查找
        $sql = "select * from `$this->sheet` where id = $id";
        $re = $this->sql($sql);
        return mysqli_fetch_assoc($re);
    }
    
    public function find($data){  //查找
        $sql = "select * from  `$this->sheet` where ";
        foreach ($data as $key => $val) {
            $sql .= " `$key`='$val' and";
        }
        $sql = substr($sql,0,-3);
        $re = $this->sql($sql);
        return mysqli_fetch_assoc($re);
    }

    public function update($data,$id){  //修改
        $sql = "update `$this->sheet` set ";
        foreach ($data as $key => $val) {
            $sql .= "`$key`='$val',";
        }
        $sql = substr($sql,0,-1)." where id=$id";
        $re = $this->sql($sql);
        return mysqli_affected_rows($this->link);
    }

    public function updates($data,$that){  //修改
        $sql = "update `$this->sheet` set ";
        foreach ($data as $key => $val) {
            $sql .= "`$key`='$val',";
        }
        $sql = substr($sql,0,-1)." where";
        foreach ($that as $key => $val) {
            $sql .= "`$key`='$val',";
        }
        $sql = substr($sql,0,-1);
        $re = $this->sql($sql);
        return mysqli_affected_rows($this->link);
    }

    public function __destruct(){ //析构函数
        if($this->link){
            mysqli_close($this->link);
        }
    }
}